package com.chinaunicom.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinaunicom.auth.contant.AuthorizationConstant;
import com.chinaunicom.auth.entity.SysUser;
import com.chinaunicom.auth.entity.User;
import com.chinaunicom.auth.mapper.SysUserMapper;
import com.chinaunicom.auth.mapper.UserMapper;
import com.chinaunicom.auth.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/7 0:28
 * Description: 自定义用户密码登录
 */

@Service
public class UserDetailServiceImpl implements SysUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 模拟数据库登录, 整合前后端登录(在请求头里面进行区分)
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /**
         // 模拟数据库
         HashMap<String, User> data = new HashMap<>();
         // 添加权限
         ArrayList<SimpleGrantedAuthority> adminGrantedAuthority = new ArrayList<>();
         adminGrantedAuthority.add(new SimpleGrantedAuthority("sys:del"));
         adminGrantedAuthority.add(new SimpleGrantedAuthority("sys:add"));
         User admin = new User("admin", passwordEncoder.encode("root"), adminGrantedAuthority);


         ArrayList<SimpleGrantedAuthority> testGrantedAuthority = new ArrayList<>();
         testGrantedAuthority.add(new SimpleGrantedAuthority("test:add"));
         testGrantedAuthority.add(new SimpleGrantedAuthority("test:query"));
         User test = new User("test", passwordEncoder.encode("root"), testGrantedAuthority);
         data.put("sys_user", admin);
         data.put("user", test);

         */

        // 获取请求属性
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 获取request
        HttpServletRequest request = servletRequestAttributes.getRequest();

        String loginType = request.getHeader(AuthorizationConstant.LOGIN_TYPE);

        if (StrUtil.isEmpty(loginType)) {
            loginType = AuthorizationConstant.USER;
        }

        switch (loginType) {
            case AuthorizationConstant.USER:
                // 前台
                return userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
            case AuthorizationConstant.SYS_USER:
                // 后台
                SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
//                if (ObjectUtil.isNotEmpty(sysUser)) {
//                     获取权限
//                    List<String> permissions = sysUserMapper.getPermissionByUserId(sysUser.getId());
//                    sysUser.setAuths(permissions);
//                }
                return sysUser;
        }
        return null;
    }

    @Override
    public UserDetails loadUserByMobile(String mobile) {

        return sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(StrUtil.isNotBlank(mobile), SysUser::getMobile, mobile));
    }
}
